const express = require('express');
const jwt = require('jsonwebtoken');
const { secret } = require('../config/global');
// 导入文件上传中间件
const { upload } = require('../utils/upload');
const { login, getMsgCode, checkToken, refreshToken } = require('./front/login');
const { patientList, patientUpdate } = require('./front/patient');
const { doctorList, doctorDetail, doctorUpdate } = require('./front/staff');
const { bannerList } = require('./front/banner');
const { appointmentList, appointmentInfo, appointmentTotal, appointmentAdd, appointmentUpdate, appointmentRemove } = require('./front/appointment');
const { commentList, commentTotal, commentAdd, commentRemove } = require('./front/comment');
const { businessList, businessInfo } = require('./front/business');
const { postsList, postsTotal, postsInfo, postsSearch, postsUpdate } = require('./front/posts');

// 创建路由实例
const router = express.Router();
// 短信验证码
router.get('/getMsgCode', getMsgCode);
// 登录
router.post('/login', login);
// 检测token是否合法
router.get('/checkToken', checkToken);
// token续费
router.post('/refreshToken', refreshToken);

// 获取医生列表
router.get('/doctorList', doctorList);
// 医生详情
router.get('/doctorDetail', doctorDetail);
// 评论列表
router.get('/commentList', commentList);
router.get('/commentTotal', commentTotal);

// 医疗机构主页
router.get('/businessList', businessList)
router.get('/businessInfo', businessInfo)

// 获取机构轮播图列表
router.get('/bannerList', bannerList);

// 健康知识
router.get('/postsList', postsList)
router.get('/postsTotal', postsTotal)
router.get('/postsInfo', postsInfo)
router.get('/postsSearch', postsSearch)
router.get('/postsUpdate', postsUpdate)

// ***********登录鉴权自定义中间件************//
router.use((req, res, next) => {
    // 通过请求对象传递登录信息
    req.loginInfo = {};
    // 获取接口基础地址
    // const url = req.url.split('?').shift().replace('/front', '');
    // 如果访问的是需要登录授权的接口
    // 通过请求头获取登录凭证token
    const { authorization } = req.headers;
    // 未授权, 禁止访问
    if (!authorization) {
        return res.send({
            code: 403,
            msg: '未通过请求头Authorization携带登录凭证token',
            data: []
        });
    }
    try {
        var decoded = jwt.verify(authorization, secret);
        if (!decoded) {
            return res.send({
                code: 403,
                msg: '登录凭证token不合法',
                data: []
            });
        }
        const { id, phone, role_id, exp } = decoded;
        // 过期
        if (exp < parseInt(Date.now() / 1000)) {
            return res.send({
                code: 403,
                msg: '登录凭证token已过期请重新登录',
                data: []
            })
        }
        // 将登录信息通过请求对象, 继续向后传递
        req.loginInfo = { id, phone, role_id }
    } catch (err) {
        // err
        return res.send({
            code: 403,
            msg: '登录凭证无效或已过期',
            data: []
        });
    }
    next();
})

// 获取患者列表
router.get('/patientList', patientList);
// 更新医生信息
router.post('/patientUpdate', upload.single('avatar'), patientUpdate);
router.post('/doctorUpdate', upload.single('avatar'), doctorUpdate);

// 预约操作
router.get('/appointmentList', appointmentList);
router.get('/appointmentInfo', appointmentInfo);
router.get('/appointmentTotal', appointmentTotal);
router.put('/appointmentAdd', appointmentAdd);
router.post('/appointmentUpdate', appointmentUpdate);
router.delete('/appointmentRemove', appointmentRemove);

// 评论操作
router.put('/commentAdd', commentAdd);
router.delete('/commentRemove', commentRemove);


module.exports = router;